// mircoApp/assist-competition/details.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    credit: 0,
    showSheet: false,
    showReward: false,
    showPoster: false,
    activeReward: 0,
    poster_url: null,
    options: [{
        name: "人气助力",
        icon: "icon-a-shandian1 f-20",
        color: "#FF6542"
      },
      {
        name: "打赏",
        icon: "icon-pay_yue f-40",
        color: "#FF9329"
      }
    ],
    balance: '',
    giftlist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData();
  },

  async getData() {
    let {
      activity_id,
      signup_record_id
    } = this.options;
    let urlStr = app.getNetAddresss('plugin.assist-competition.frontend.assist-voting-detail.index');
    let response = await app._postNetWork({
      url: urlStr,
      data: {
        activity_id,
        signup_record_id
      }
    });
    let {
      result,
      msg,
      data
    } = response.data;

    if (result == 0) return app.tips(msg);
    this.setData({
      info: data
    });
    wx.setNavigationBarTitle({
      title: this.data.info.participant_info && this.data.info.participant_info.name
    })
    try {
      let value = wx.getStorageSync('balance');
      value = value || "余额";
      if (value) {
        this.setData({
          balance: value
        });
      }
    } catch (e) {
      console.log(e);
    }
  },
  onClickOptions(event) {
    let {
      index
    } = event.currentTarget.dataset;
    if (index == 0) this.helpVote();
    if (index == 1) this.getGift();
  },
  async helpVote() {
    let {
      activity_id,
      signup_record_id
    } = this.options;
    let urlStr = app.getNetAddresss('plugin.assist-competition.frontend.assist-voting-detail.assist-action');
    let response = await app._postNetWork({
      url: urlStr,
      data: {
        activity_id,
        signup_record_id
      }
    });
    let {
      result,
      msg,
      data
    } = response.data;
    app.tips(msg);
    this.getData();
  },
  async getGift() {
    let {
      activity_id,
      signup_record_id
    } = this.options;
    let urlStr = app.getNetAddresss('plugin.assist-competition.frontend.assist-voting-detail.get-tip-gifts-list');
    let response = await app._postNetWork({
      url: urlStr,
      data: {
        activity_id,
        signup_record_id
      }
    });
    let {
      result,
      msg,
      data
    } = response.data;

    if (result == 0) return app.tips(msg);
    let setData = {};
    setData.giftlist = data.data;
    setData.credit = data.credit;
    setData.activeReward = data?.data[0]?.id || 0;
    setData.showReward = true;
    this.setData(setData);
  },
  onSelectGigt(event) {
    let {
      id
    } = event.currentTarget.dataset;
    this.setData({
      activeReward: id
    });
  },
  async toReward() {
    if (!this.data.activeReward) return app.tips('请选择打赏礼物');

    let {
      activity_id,
      signup_record_id
    } = this.options;
    let urlStr = app.getNetAddresss('plugin.assist-competition.frontend.assist-voting-detail.tip-action');
    let response = await app._postNetWork({
      url: urlStr,
      data: {
        activity_id,
        signup_record_id,
        tip_gifts_id: this.data.activeReward
      }
    });
    let {
      result,
      msg,
      data
    } = response.data;

    
    if (result == 0) {
      if (data.err_scope) {
        wx.showModal({
          content: `${this.data.balance}不足`,
          confirmText: '去充值',
          complete: (res) => {
            if (res.cancel) {

            }

            if (res.confirm) {
              //余额不足，去充值余额
              wx.navigateTo({
                url: "/packageA/member/balance/balance_recharge/balance_recharge"
              });
            }
          }
        });
      }
      return;
    }
    app.tips(msg);
    this.getData();
    this.setData({
      showSheet: false,
      showReward: false
    })
  },
  async getPoster() {
    let {
      activity_id,
      signup_record_id
    } = this.options;
    let urlStr = app.getNetAddresss('plugin.assist-competition.frontend.assist-voting-detail.get-poster');
    let response = await app._postNetWork({
      url: urlStr,
      data: {
        activity_id,
        signup_record_id,
      }
    });
    let {
      result,
      msg,
      data
    } = response.data;

    if (result == 0) return app.tips(msg);
    this.setData({
      poster_url: data.poster,
      showPoster: true
    });
  },
  onClistShow(event) {
    let {
      name,
      val
    } = event.currentTarget.dataset;
    this.setData({
      [name]: val
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})